package net.caixiaomi.info.Lottery.ui;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import net.caixiaomi.info.Lottery.model.LotteryHistoryEntity;
import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;

import java.util.List;

/**
 * Created by suntian on 2018/8/10.
 */

public class HistoryPopupWindow {
    
    private Context mContext;
    private View mHistoryListView;
    private PopupWindow mHistoryPopup;
    public RecyclerView mListView;
    public HistoryAdapter mAdapter;
    
    public void init(Context context){
        mContext=context;
        mHistoryListView=View.inflate(mContext, R.layout.lottery_history_list_layout,null);
        mListView=mHistoryListView.findViewById(R.id.lottery_history_listview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        mListView.setLayoutManager(layoutManager);
        mAdapter=new HistoryAdapter(R.layout.lottery_history_list_item);
    }

    public void setData(List<LotteryHistoryEntity> list){
        mAdapter.setNewData(list);
        mAdapter.notifyDataSetChanged();
        mListView.setAdapter(mAdapter);
    }

    private class HistoryAdapter extends BaseQuickAdapter<LotteryHistoryEntity,BaseViewHolder> {


        public HistoryAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, LotteryHistoryEntity item) {
            helper.setText(R.id.history_title, item.getTermNum());
            LinearLayout group=helper.getView(R.id.history_num_group);
            for(int i=0;i<group.getChildCount();i++){
                TextView itemTv= (TextView) group.getChildAt(i);
                itemTv.setText(item.getNumList().get(i));
            }
        }
    }

    public void showHistoryPopupWindow(View view){

        if (mHistoryPopup == null) {
            mHistoryPopup = new PopupWindow(mContext);
        }

        mHistoryPopup.setFocusable(true);
        if(mHistoryPopup.isShowing()){
            mHistoryPopup.dismiss();
            return;
        }

        mHistoryPopup.setWidth(CommonApp.mScreenWidth);
        mHistoryPopup.setHeight(3*CommonApp.mScreenHeight/5);
        mHistoryPopup.setContentView(mHistoryListView);

        mHistoryPopup.setOutsideTouchable(true);
        mHistoryPopup.setBackgroundDrawable(null);
        mHistoryPopup.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        // 设置背景颜色变暗
        final Activity act= (Activity) mContext;
        WindowManager.LayoutParams lp = act.getWindow().getAttributes();
        lp.alpha = 0.7f;
        act.getWindow().setAttributes(lp);
        mHistoryPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = act.getWindow().getAttributes();
                lp.alpha = 1f;
                act.getWindow().setAttributes(lp);
            }
        });

        TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(200);
        mHistoryListView.startAnimation(animation);
    }
}
